﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EPiServer.Core;
using EPiServer.Municipality.Web.UI;
using EPiServer.Municipality.Configuration;
using EPiServer.Municipality.Extensions;

namespace EPiServer.Municipality.Templates.MobilePages {

    public partial class EventList : TemplatePage {

        public string GetIntroText(PageData page)
        {
            if (page == null)
                return string.Empty;
            return string.Format("<p class='listtext'>{0}</p>", page["IntroText"]);
           
        }
       
        public PageDataCollection Pages { 
            get{
                if (_pages == null){
                    _pages = DataFactory.Instance.GetChildren(CurrentPage.PageLink);
                }
                return _pages;    
            } 
        }
        private PageDataCollection _pages;
        
        
        protected string GetDate(object o)
        {
            var page = (PageData)o;

            return ((DateTime)page["EventStart"]).ToString("HH:ss");
        }

        protected string GetClass(string mode)
        {
            if (mode == Request.QueryString["mode"])
                return "class=\"" + "ui-btn-active" + "\"";

           else if (string.IsNullOrEmpty(Request.QueryString["mode"]) && mode == Constants.EventMode.week.ToString())

                return "class=\"" + "ui-btn-active" + "\"";

            return string.Empty;
        }

        private int DaysToRender()
        {
            var mode = Request["mode"];
            if(mode == Constants.EventMode.month.ToString())
                return (int)Constants.EventMode.month;
            if(mode == Constants.EventMode.year.ToString())
                return (int)Constants.EventMode.year;
            
            return (int)Constants.EventMode.week;
         }
        protected string GetEventCount()
        {
            var days = DaysToRender();
            var curDate = (DateTime)Pages[0]["EventStart"] > DateTime.Now ? (DateTime)Pages[0]["EventStart"] : DateTime.Now;
            curDate = new DateTime(curDate.Year, curDate.Month, curDate.Day, 0, 0, 0);
            var events = Pages.Where(p => (((DateTime)p["EventStop"]).Date >= curDate) && (((DateTime)p["EventStart"]).Date < curDate.AddDays(days))).ToList();
            
            
            //var events = pages.Where(p => (((DateTime)p["EventStart"]).Date <= curDate && ((DateTime)p["EventStop"]).Date >= curDate)).ToList();
            return events.Count.ToString();
 
        }
       
        protected string GetEventDatePeriod()
        {
         
            if (Pages.Count > 0)
            {
                var orderedPages = Pages.OrderBy(p => p["EventStart"]).ToList();
                DateTime date = (DateTime)orderedPages[0]["EventStart"] > DateTime.Now ? (DateTime)orderedPages[0]["EventStart"] : DateTime.Now;
                return string.Format("evenemang mellan {0} - {1}", date.ToString("d"), date.AddDays(DaysToRender()).ToString("d"), Pages.Count);
            }

            //else if (Request.QueryString["mode"] == "month") 
            //{
            //    var orderedPages = Pages.OrderBy(p => p["EventStart"]).ToList();
            //    DateTime date = (DateTime)orderedPages[0]["EventStart"] > DateTime.Now ? (DateTime)orderedPages[0]["EventStart"] : DateTime.Now;
            //    return string.Format("Visar för datumen {0} - {1}", date.ToString("d"), date.AddDays(30).ToString("d"));
            
            //}

            //else if (Request.QueryString["mode"] == "year")
            //{
            //    var orderedPages = Pages.OrderBy(p => p["EventStart"]).ToList();
            //    DateTime date = (DateTime)orderedPages[0]["EventStart"] > DateTime.Now ? (DateTime)orderedPages[0]["EventStart"] : DateTime.Now;
            //    return string.Format("Visar för datumen {0} - {1}", date.ToString("d"), date.AddDays(365).ToString("d"));

            //}

            return string.Empty;
        }

        
        protected void Page_Load(object sender, EventArgs e) 
        {
            EventRepeater.PageLink = CurrentPage.PageLink;
            EventRepeater.DataBind();

        }

 }
}
